package servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


public class UploadServelt extends HttpServlet {
	private static final long serialVersionUID = 1L;
   
    public UploadServelt() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DiskFileItemFactory factory=new DiskFileItemFactory(20*1024, new File("/Users/john/temp1/"));
		ServletFileUpload upload=new ServletFileUpload(factory);
		upload.setSizeMax(80*1024);
		
		try {
			List<FileItem> items= upload.parseRequest(request);
			for(FileItem item:items){
				if(item.isFormField()){
					System.out.println(item.getFieldName()+"..."+item.getString());
				}
				else{
					System.out.println(item.getName()+"...."+item.getSize());
					InputStream fin=item.getInputStream();
					
				}
			}
			
		} catch (FileUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}

}





